package org.jeecg.modules.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.modules.doctor.entity.Doctor;
import org.jeecg.common.modules.doctor.entity.DoctorAccount;
import org.jeecg.common.modules.doctor.service.IDoctorService;
import org.jeecg.common.modules.makeAppointment.entity.MakeAppointment;
import org.jeecg.common.modules.makeAppointment.entity.MakeAppointmentInfo;
import org.jeecg.common.modules.makeAppointment.service.IMakeAppointmentInfoService;
import org.jeecg.common.modules.makeAppointment.service.IMakeAppointmentService;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.UidUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.*;

/**
* @Description: 配置预约主表
* @Author: jeecg-boot
* @Date:   2021-09-01
* @Version: V1.0
*/
@Api(tags="配置预约主表")
@RestController
@RequestMapping("/makeAppointment/makeAppointment")
@Slf4j
public class MakeAppointmentController extends JeecgController<MakeAppointment, IMakeAppointmentService> {
   @Autowired
   private IMakeAppointmentService makeAppointmentService;

   @Autowired
   private IMakeAppointmentInfoService  iMakeAppointmentInfoService;
   @Autowired
   private IDoctorService IDoctorService;
   /**
    * 分页列表查询
    *
    * @param makeAppointment
    * @param pageNo
    * @param pageSize
    * @param req
    * @return
    */
   @AutoLog(value = "配置预约主表-分页列表查询")
   @ApiOperation(value="配置预约主表-分页列表查询", notes="配置预约主表-分页列表查询")
   @GetMapping(value = "/list")
   public Result<?> queryPageList(MakeAppointment makeAppointment,
                                  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                  HttpServletRequest req) {
       QueryWrapper<MakeAppointment> queryWrapper = QueryGenerator.initQueryWrapper(makeAppointment, req.getParameterMap());
       Page<MakeAppointment> page = new Page<MakeAppointment>(pageNo, pageSize);
       IPage<MakeAppointment> pageList = makeAppointmentService.page(page, queryWrapper);
       return Result.ok(pageList);
   }

   /**
    *   添加
    *
    * @param makeAppointment
    * @return
    */
   @AutoLog(value = "配置预约主表-添加")
   @ApiOperation(value="配置预约主表-添加", notes="配置预约主表-添加")
   @PostMapping(value = "/add")
   public Result<?> add(@RequestBody MakeAppointment makeAppointment,@RequestParam("doctorId") String doctorId) {
       //计算分钟数
       long startTime = makeAppointment.getStartTime().getTime();
       long endTime = makeAppointment.getEndTime().getTime();
       long min = (endTime - startTime);
       //生成对应的出诊时间段
       int minSplite = (int)min/makeAppointment.getTotal();
       List<MakeAppointmentInfo> list = new ArrayList<>();
       int i = 0;
       String uid = getUid32();
       Doctor doctor = IDoctorService.getById(doctorId);
       while(i != makeAppointment.getTotal()){
           MakeAppointmentInfo makeAppointmentInfo = new MakeAppointmentInfo();
           makeAppointmentInfo.setStartTime(new Date(startTime));
           makeAppointmentInfo.setEndTime(new Date(startTime+ minSplite));
           makeAppointmentInfo.setMakeAppointmentId(uid);
           makeAppointmentInfo.setStatus(0);
           makeAppointmentInfo.setDoctorId(doctorId);
           makeAppointmentInfo.setDoctorName(doctor.getName());
           makeAppointmentInfo.setDate(makeAppointment.getDate());
           startTime = startTime+ minSplite;
           list.add(makeAppointmentInfo);
           i++;
       }
       iMakeAppointmentInfoService.saveBatch(list);
       makeAppointment.setId(uid);
       makeAppointment.setDoctorId(doctorId);
       makeAppointment.setSurplus(makeAppointment.getTotal());
       makeAppointment.setDoctorNanme(doctor.getName());
       makeAppointmentService.save(makeAppointment);
       return Result.ok("添加成功！");
   }

   /**
    *  编辑
    *
    * @param makeAppointment
    * @return
    */
   @AutoLog(value = "配置预约主表-编辑")
   @ApiOperation(value="配置预约主表-编辑", notes="配置预约主表-编辑")
   @PutMapping(value = "/edit")
   public Result<?> edit(@RequestBody MakeAppointment makeAppointment) {
       makeAppointmentService.updateById(makeAppointment);
       return Result.ok("编辑成功!");
   }

   /**
    *   通过id删除
    *
    * @param id
    * @return
    */
   @AutoLog(value = "配置预约主表-通过id删除")
   @ApiOperation(value="配置预约主表-通过id删除", notes="配置预约主表-通过id删除")
   @DeleteMapping(value = "/delete")
   public Result<?> delete(@RequestParam(name="id",required=true) String id) {
       makeAppointmentService.removeById(id);
       return Result.ok("删除成功!");
   }

   /**
    *  批量删除
    *
    * @param ids
    * @return
    */
   @AutoLog(value = "配置预约主表-批量删除")
   @ApiOperation(value="配置预约主表-批量删除", notes="配置预约主表-批量删除")
   @DeleteMapping(value = "/deleteBatch")
   public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
       this.makeAppointmentService.removeByIds(Arrays.asList(ids.split(",")));
       return Result.ok("批量删除成功!");
   }

   /**
    * 通过id查询
    *
    * @param id
    * @return
    */
   @AutoLog(value = "配置预约主表-通过id查询")
   @ApiOperation(value="配置预约主表-通过id查询", notes="配置预约主表-通过id查询")
   @GetMapping(value = "/queryById")
   public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
       MakeAppointment makeAppointment = makeAppointmentService.getById(id);
       if(makeAppointment==null) {
           return Result.error("未找到对应数据");
       }
       return Result.ok(makeAppointment);
   }

   /**
   * 导出excel
   *
   * @param request
   * @param makeAppointment
   */
   @RequestMapping(value = "/exportXls")
   public ModelAndView exportXls(HttpServletRequest request, MakeAppointment makeAppointment) {
       return super.exportXls(request, makeAppointment, MakeAppointment.class, "配置预约主表");
   }

   /**
     * 通过excel导入数据
   *
   * @param request
   * @param response
   * @return
   */
   @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
   public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
       return super.importExcel(request, response, MakeAppointment.class);
   }
    /**
     * 获取uuid32
     * @return
     */
    public static String getUid32(){
        String uuid = UUID.randomUUID().toString();
        uuid = uuid.replace("-","");
        return uuid;
    }
}
